package cn.itsnkkka.producer;

import cn.itsnkkka.domain.dto.RabbitMQObjectDTO;
import cn.itsnkkka.domain.enums.RabbitMQMessageTypeEnum;
import cn.itsnkkka.holder.SpringUtils;
import cn.itsnkkka.utils.JSONUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;


/**
 * @author xuezipeng
 * @date 2023-04-23 12:35
 */
public abstract class AbstractProducerTemplate implements IProducer{
    protected boolean sendMessage(RabbitMQMessageTypeEnum messageTypeEnum, final RabbitMQObjectDTO message){
        RabbitTemplate rabbitTemplate = SpringUtils.getBean(RabbitTemplate.class);
        try{
            rabbitTemplate.convertAndSend(
                    messageTypeEnum.getExchangeName(),
                    messageTypeEnum.getRoutingKeyName(),
                    JSONUtils.toJSONString(message)
            );
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
